package com.db.sql.method;

import java.util.List;
import java.util.Map;

import com.db.ColumnInfoBean;
import com.db.sql.Node;
import com.db.sql.NodeParameter;
import com.db.sql.SqlLeafNode;
import com.db.sql.SqlNode;
import com.excel.read.Config;
import com.excel.read.GridSearchTable;

public class UpSearchMethod {

	/**
	 * 
	 * control是本地字段
	 */
	public NodeParameter getSql(GridSearchTable gst, SqlNode sqlNode, Map keyValue, Map <String, String>params, String control) {
		NodeParameter nodeParameter = new NodeParameter();
		int size = params.keySet().size();
		//单独节点
	
			//顶层节点
			Object[] keys = params.keySet().toArray();
			//varName
			String key = (String) keys[0];
			String value = params.get(key);
			//注入SQL节点
			String fullPath = (String) keyValue.get(control.trim());
			List<Node> aNodeList =  gst.getSqlNode(fullPath, control.trim(), sqlNode);
			SqlLeafNode  sqlLeafNode = (SqlLeafNode) aNodeList.get(aNodeList.size()-1);
			
			
			
			//表名
			String tableName = sqlLeafNode.getEntity().getNodeName();			
		
			//父表
			String pTableName = sqlLeafNode.getEntity().getPrev().getNodeName();
			
			
			//查询字段、			
			ColumnInfoBean cfb = Config.getBean(tableName, sqlLeafNode.getNodeName());

			//主键
			ColumnInfoBean cfbPK = Config.getPKBean(tableName);
			nodeParameter.setBeans(new ColumnInfoBean[]{cfbPK, cfb});
			
			
			
			//SQL
			String sql = "select " + cfbPK.getName()+", "+sqlLeafNode.getNodeName() + " from " + tableName;
			nodeParameter.setSql(sql);
			
			nodeParameter.setQueryMethod(NodeParameter.SINGLE_FIELD_QUERY);
			
		
		return nodeParameter;
	}
	
}
